﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VatTuDTO;
using VatTuBUS;

namespace CungUngXayDung
{
    public partial class GioHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                //Lấy ConString
                int MaSP = int.Parse(Request.QueryString["id"].ToString());
                List<GioHangDTO> list = new List<GioHangDTO>();
                list = (List<GioHangDTO>)Session["Cart"];
                GioHangBUS bus = new GioHangBUS();

                if (bus.KiemTra(list, MaSP) == true)
                {
                    lbTrangThai.Text = "Sản phẩm đã có trong giỏ hàng, nếu muốn mua thêm hãy cập nhật số lượng !";
                    lbTrangThai.ForeColor = System.Drawing.Color.White;
                    lbTrangThai.BackColor = System.Drawing.Color.Red;
                }
                else
                {

                    list = bus.ThemSanPhamGioHang(list, MaSP);
                    Session["Cart"] = list;
                }
            }
            if (Request.QueryString["delid"] != null)
            {
                //Lấy ConString
                int MaSP = int.Parse(Request.QueryString["delid"].ToString());
                List<GioHangDTO> list = new List<GioHangDTO>();
                list = (List<GioHangDTO>)Session["Cart"];
                Session["Cart"] = new GioHangBUS().XoaSanPhamGioHang(list, MaSP);
            }

            LoadGioHang();
        }
        public void LoadGioHang()
        {
            List<GioHangDTO> list = new List<GioHangDTO>();
            list = (List<GioHangDTO>)Session["Cart"];

            ShopCart.DataSource = list;
            ShopCart.DataBind();

            lbTongGiaTri.Text = new GioHangBUS().TinhTongTienGioHang(list).ToString();
            lbTongGiaTri2.Text = lbTongGiaTri.Text;

            if (list==null|| list.Count == 0)
            {
                lbTrangThai.Text = "Giỏ hàng rỗng !!!";
            }
        }




        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            List<GioHangDTO> list = new List<GioHangDTO>();
            list = (List<GioHangDTO>)Session["Cart"];

            int i = 2;
            int j = 1;
            int MaSP;
           int SoLuong;
            String TenControl;

            String error = "";

            while (j <= list.Count)
            {
                TenControl = Request.Form.AllKeys[i++].ToString();
                MaSP = int.Parse(Request.Form[TenControl].ToString());
                TenControl = Request.Form.AllKeys[i++].ToString();
                SoLuong = int.Parse(Request.Form[TenControl].ToString());

                if (SoLuong < 0)
                {
                    error = error + MaSP + "_";
                }
                else
                {
                    list = new GioHangBUS().CapNhatGioHang(list, MaSP, SoLuong);
                }

                j++;
            }

            Session["Cart"] = list;

            if (error != "")
            {
                Response.Redirect("GioHang.aspx?error=" + error);
            }
            else
            {
                Response.Redirect("GioHang.aspx");
            }
        }
        protected void btnThanhToan_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThanhToan.aspx");
        }

        protected void btnDatHang_Click(object sender, EventArgs e)
        {
            Response.Redirect("DatHang.aspx");
        }
    }
}